package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.SpecVO;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.TbSpecificationService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("spec")
public class TbSpecificationController {

    // 参数在 controller 层处理    业务代码 service 层

    // dubbo 的注解
    @Reference
    private TbSpecificationService tbSpecificationService;

    @RequestMapping("list")
    public String list(){
        return "admin/specification";
    }

    @RequestMapping("getSpecInfo")
    @ResponseBody
    public Result getSpecInfo(String specName){
        List<TbSpecification> tbSpec = tbSpecificationService.getSpecInfo(specName);
        return Result.build(ConstantEnum.SUCCESS).setResult(tbSpec);
    }


    @RequestMapping("insertSpecInfo")
    @ResponseBody
    public Result insertSpecInfo(TbSpecification tbSpecification,String optionListJson){
        if (tbSpecification == null && optionListJson == null){
            return Result.build(ConstantEnum.ARCS_NULL);
        }
        // 第一个参数 : 要转换的参数   第二个参数 : 要转换的类型 泛型
        List<TbSpecificationOption> options = JSONObject.parseArray(optionListJson, TbSpecificationOption.class);
        tbSpecificationService.insertSpecInfo(tbSpecification,options);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("updateSpec/{id}")
    @ResponseBody
    public SpecVO updateSpec(@PathVariable("id")Long id){
        SpecVO specVO = new SpecVO();
        TbSpecification s = tbSpecificationService.updateSpec(id);
        specVO.setTbSpecification(s);
        List<TbSpecificationOption> sp = tbSpecificationService.selectBySpecOp(id);
        specVO.setList(sp);
        return specVO;
    }

    @RequestMapping("updateSpecInfo")
    @ResponseBody
    public Result updateSpecInfo(TbSpecification tbSpecification,String optionListJson){
        System.out.println(optionListJson);
        // 第一个参数 : 要转换的参数   第二个参数 : 要转换的类型 泛型
        List<TbSpecificationOption> options = JSONObject.parseArray(optionListJson, TbSpecificationOption.class);
        tbSpecificationService.updateSpecInfo(tbSpecification,options);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("deleteSpecInfo")
    @ResponseBody
    public Result deleteSpecInfo(@RequestParam("ids")Long[] ids){
        if (ids == null){
            return Result.build(ConstantEnum.ARCS_NULL);
        }
        tbSpecificationService.deleteSpecInfo(ids);
        return Result.build(ConstantEnum.SUCCESS);
    }
}
